package com.cjx.Controller;

import com.cjx.Service.BookService;
import com.cjx.pojo.Books;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cjx
 * @date 2021/3/23 0023 - 21:44
 */
@Controller
@RequestMapping("/book")
public class BookController {
    //controller调service
    @Resource
    private BookService bookService;

    //查询全部书籍，并返回到页面
    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> books = bookService.selectAll();
        model.addAttribute("list", books);
        System.out.println("allBook");
        return "allBook";
    }

    //书记增加页面
    @RequestMapping("/toAddBook")
    public String toAddPaper() {
        System.out.println("toAddBook");
        return "addBook";
    }

    //添加书籍请求
    @RequestMapping("/addBook")
    public String addbook(Books books) {
        bookService.addBook(books);
        System.out.println("addBook");
        return "redirect:/book/allBook";
    }
    //修改页面

    //    @RequestMapping  ("/toUpdateBook")
//    public String toUpdateBook(Model model, int id) {
//        Books books = bookService.selectById(id);
//        System.out.println(books);
//        model.addAttribute("book",books );
//        return "updateBook";
//    }
    @RequestMapping("/toUpdateBook")
    public String toUpdatePaper(int id, Model model) {
        Books books = bookService.selectById(id);
        model.addAttribute("book", books);
        System.out.println("toUpdateBook");
        return "updateBook";
    }

    //修改书籍
    @RequestMapping("/updateBook")
    public String updateBook(Books books) {

        bookService.updateBook(books);
        return "redirect:/book/allBook";

    }
    //删除书籍

    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }

    //查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String BookName, Model model) {
        Books books = bookService.selectBookByName(BookName);
        List<Books> list = new ArrayList<Books>();
        list.add(books);
        System.out.println(books);
        if (books == null) {
            return "redirect:/book/allBook";
        }
        model.addAttribute("list", list);
        return "allBook";
    }

}
